﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/TemplateClassify")]
    public class TemplateClassifyController : ApiController
    {
        B_TemplateClassify b;
        public TemplateClassifyController()
        {
            b =new B_TemplateClassify();
        }

        /// <summary>
        /// 带分类参数的获取所有模板分类
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll(int page,int size)
        {
            var data = from i in b.GetAll()
                       orderby i.ClassifyID ascending
                       select new TemplateClassify
                       {
                           ClassifyID = i.ClassifyID,
                           Cname = i.Cname,
                           Show = i.Show,
                           Icon = i.Icon,
                           Sort = i.Sort,
                           ParentID = i.ParentID,
                       };
            return Ok(new Result<List<TemplateClassify>> {
                Data = data.Skip((page-1)*size).Take(size).ToList(),
                Msg=data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有模板分类
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            var data = from i in b.GetAll()
                       orderby i.ClassifyID ascending
                       select new TemplateClassify
                       {
                           ClassifyID = i.ClassifyID,
                           Cname = i.Cname,
                           Show = i.Show,
                           Icon = i.Icon,
                           Sort = i.Sort,
                           ParentID = i.ParentID
                       };
            return Ok(new Result<List<TemplateClassify>>
            {
                Data = data.ToList()
            });
        }
        /// <summary>
        /// 模板分类分级显示
        /// </summary>
        /// <returns></returns>
        [Route("LevelClass")]
        [HttpGet]
        public IHttpActionResult LevelClass()
        {
            var data = from t in b.LevelClass()
                       select new LevelClass
                       {
                           LevelName = t.LevelName,
                           ListTemplateClassify = (from i in t.ListTemplateClassify
                                                   select new Param_TemplateClassify
                                                   {
                                                       ClassifyID = i.ClassifyID,
                                                       Cname = i.Cname,
                                                       Show = i.Show,
                                                       Icon = i.Icon,
                                                       Sort = i.Sort,
                                                       ParentID = i.ParentID
                                                   }).ToList()
                       };
            return Ok(new Result<List<LevelClass>>
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 根据ID查询指定分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByID(int id)
        {
            try
            {
                var data =b.FindById(id);
                if (data==null)
                {
                    return Ok(new Result<object> { Code = 404, Msg = "数据为空" });
                }
                return Ok(new Result<TemplateClassify>
                {
                    Data = new TemplateClassify
                    {
                        ClassifyID = data.ClassifyID,
                        Cname = data.Cname,
                        Show = data.Show,
                        Icon = data.Icon,
                        Sort = data.Sort,
                        ParentID = data.ParentID,
                    }
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 500, Msg = "出现错误，请联系管理员" });
            }
        }

        /// <summary>
        /// 获取当前分类的所有父级分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("ParentClass/{id}")]
        [HttpGet]
        public IHttpActionResult ParentClass(int id)
        {
            var data = from tc in b.ParentClass(id)
                       select new Param_TemplateClassify {
                           ClassifyID=tc.ClassifyID,
                           Cname=tc.Cname,
                           Show=tc.Show,
                           Icon=tc.Icon,
                           Sort=tc.Sort,
                           ParentID=tc.ParentID
                       };
            return Ok(new Result<List<Param_TemplateClassify>>() {
                Data=data.ToList()
            });
        }

        /// <summary>
        /// 查询所有一级分类
        /// </summary>
        /// <returns></returns>
        [Route("RootClass")]
        [HttpGet]
        public IHttpActionResult RootClass()
        {
            var data = from tc in b.RootClass()
                       select new Param_TemplateClassify
                       {
                           ClassifyID = tc.ClassifyID,
                           Cname = tc.Cname,
                           Show = tc.Show,
                           Icon = tc.Icon,
                           Sort = tc.Sort,
                           ParentID = tc.ParentID
                       };
            return Ok(new Result<List<Param_TemplateClassify>>()
            {
                Data = data.ToList()
            });
        }


        /// <summary>
        /// 根据ID删除指定模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                b.Delete(id);
                return Ok(new Result<object> { });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
            }
        }

        /// <summary>
        /// 添加模板
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(Param_TemplateClassify param)
        {
            //var data=HttpHelper.HttpPost<Result<string>{ 图片服务器地址，匿名函数键值对}>
            try
            {
                b.Add(new TemplateClassify
                {
                    ClassifyID = param.ClassifyID,
                    Cname = param.Cname,
                    Show = param.Show,
                    Icon = param.Icon,
                    Sort = param.Sort,
                    ParentID = param.ParentID,
                });
                return Ok(new Result<object> { });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
            }
        }

        /// <summary>
        /// 更新模板
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpPost]
        public IHttpActionResult Update(int id, Param_TemplateClassify param)
        {
            //var data=HttpHelper.HttpPost<Result<string>{ 图片服务器地址，匿名函数键值对}>
            try
            {
                b.Update(id, new TemplateClassify {
                    ClassifyID = param.ClassifyID,
                    Cname = param.Cname,
                    Show = param.Show,
                    Icon = param.Icon,
                    Sort = param.Sort,
                    ParentID = param.ParentID,
                });
                return Ok(new Result<object> { });
            }
            catch (Exception)
            {
                return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
            }
        }
    }
}
